<template>
  <div>
    <el-scrollbar style="height: 470px" v-if="traceArr.length > 0">
      <div class="imglist-box">
        <div class="item" v-for="item in traceArr" :key="item.id" @click="seeTimeLine(item.id,item.day)">
          <el-image style="width: 188px; height: 188px" :src="item.picPath" fit="fit" :lazy="true"></el-image>
          <div class="time">{{ item.day }}</div>
        </div>
      </div>
    </el-scrollbar>
    <div v-else>
      <el-empty :image-size="200" description="暂无轨迹"></el-empty>
    </div>
    <div class="pagination-box" v-if="traceArr.length > 0">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNo" :page-sizes="pageSizeArr" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getUsertraceList } from "@/api/home";

export default {
  props: {
    userId: {
      type: Number,
      default() {
        return 0;
      }
    }
  },
  data() {
    return {
      pageNo: 1,
      pageSize: 20,
      pageSizeArr: [10, 20, 30, 40, 50, 60],
      totalCount: 0,
      traceArr: []
    };
  },
  watch: {
    userId(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.getUserTraceArr();
      }
    }
  },

  mounted() {
    this.getUserTraceArr();
  },
  methods: {
    async getUserTraceArr() {
      if (this.userId <= 0) {
        return;
      }
      let res = await getUsertraceList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        userId: this.userId
      });
      if (res.code == 200) {
        console.log(8888,res.data);
        this.traceArr = res.data.lists;
        this.totalCount = res.data.count;
      }
    },

    handleSizeChange(e) {
      this.pageSize = e;
      this.pageNo = 1;
      this.traceArr = [];
      this.getUserTraceArr();
    },

    handleCurrentChange(e) {
      this.pageNo = e;
      this.traceArr = [];
      this.getUserTraceArr();
    },

    seeTimeLine(id,day) {
      this.$emit("getRaceId", { trace_id: id, day: day });
    }
  }
};
</script>

<style lang="scss" scoped>
.imglist-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: 24px;
}

.item {
  flex: 0 0 19%;
  text-align: center;
  /* 边距，css函数代替 */
  margin-right: calc(5% / 4);
  margin-bottom: calc(5% / 4);
  display: flex;
  flex-direction:column;
  .time{
    position: relative;
    bottom: -7px;
    font-size: 14px;
    color: rgba(255, 255, 255, 1);
    height: 20px;
  }
}

/* 去除每行尾的多余边距 */
.item:nth-child(5n) {
  margin-right: 0;
}

/* 使最后一个元素的边距填满剩余空间 */
.item:last-child {
  margin-right: auto;
}

.pagination-box {
  width: 100%;
  text-align: right;
  margin-top: 10px;
}
</style>
